# -*- coding:utf-8 -*-

from typing import List
from datetime import datetime, time
from module_admin.entity.do.role_do import SysRoleDept
from sqlalchemy import and_, delete, desc, func, or_, select, update
from sqlalchemy.ext.asyncio import AsyncSession
from module_gen.constants.gen_constants import GenConstants

from module_stock.entity.do.dc_index_do import DcIndex
from module_stock.entity.vo.dc_index_vo import DcIndexPageModel, DcIndexModel
from utils.page_util import PageUtil, PageResponseModel


class DcIndexDao:

    @classmethod
    async def get_by_id(cls, db: AsyncSession, dc_index_id: int) -> DcIndex:
        """根据主键获取单条记录"""
        dc_index = (((await db.execute(
                            select(DcIndex)
                            .where(DcIndex.id == dc_index_id)))
                       .scalars())
                       .first())
        return dc_index

    """
    查询
    """
    @classmethod
    async def get_dc_index_list(cls, db: AsyncSession,
                             query_object: DcIndexPageModel,
                             data_scope_sql: str = None,
                             is_page: bool = False) -> [list | PageResponseModel]:

        query = (
            select(DcIndex)
            .where(
                DcIndex.leading.like(f"%{query_object.leading}%") if query_object.leading else True,
                DcIndex.leading_code == query_object.leading_code if query_object.leading_code else True,
                DcIndex.name.like(f"%{query_object.name}%") if query_object.name else True,
                DcIndex.ts_code == query_object.ts_code if query_object.ts_code else True,
                eval(data_scope_sql) if data_scope_sql else True,
            )
            # .order_by(desc(DcIndex.create_time))
            # .distinct()
        )
        dc_index_list = await PageUtil.paginate(db, query, query_object.page_num, query_object.page_size, is_page)
        return dc_index_list


    @classmethod
    async def add_dc_index(cls, db: AsyncSession, add_model: DcIndexModel, auto_commit: bool = True) -> DcIndex:
        """
        增加
        """
        dc_index =  DcIndex(**add_model.model_dump(exclude_unset=True))
        db.add(dc_index)
        await db.flush()
        if auto_commit:
            await db.commit()
        return dc_index

    @classmethod
    async def edit_dc_index(cls, db: AsyncSession, edit_model: DcIndexModel, auto_commit: bool = True) -> DcIndex:
        """
        修改
        """
        edit_dict_data = edit_model.model_dump(exclude_unset=True, exclude={*GenConstants.DAO_COLUMN_NOT_EDIT})
        await db.execute(update(DcIndex), [edit_dict_data])
        await db.flush()
        if auto_commit:
            await db.commit()
        return await cls.get_by_id(db, edit_model.id)

    @classmethod
    async def del_dc_index(cls, db: AsyncSession, dc_index_ids: List[str], soft_del: bool = True, auto_commit: bool = True):
        """
        删除
        """
        if soft_del:
            await db.execute(update(DcIndex).where(DcIndex.id.in_(dc_index_ids)).values(del_flag='2'))
        else:
            await db.execute(delete(DcIndex).where(DcIndex.id.in_(dc_index_ids)))
        await db.flush()
        if auto_commit:
            await db.commit()